#! /bin/bash

# --------------------------------------------------------------
# Filename : update_repo.sh
# Version  : "0.1"
# Date     : "2007-06-22 09:45:22"
# Author   : "Jiahuan Li <huanlf@gmail.com>"
# License  : "Licensed under the GPL v2"
# Copyright: "Copyright (C) 2007 by Jiahuan Li <huanlf@gmail.com>"
# 
# Description: to update Package and Release in to apt repo tree
# +            this script must be run after new debs added or old debs deleted
# --------------------------------------------------------------

set -e # exit if any error

gpg_keyid='xxxxxxxx'
apt_root='/var/www/apt'

dists="dapper edgy feisty"
sessions="main"
bin_arch="binary-i386"
aptconf="$(dirname $0)/aptconf'

export HOME='YOUR_HOME'
export LANG='C'

if [ "$1" ]; then
    case $1 in 
    dapper)
        dists=$1;;
    feisty)
        dists=$1;;
    esac
fi

cd $apt_root || exit 1

for d in $dists; do
    for s in $sessions; do
        for b in $bin_arch; do

            dir="dists/$d/$s/$b"
            [ -d $dir ] || continue
            echo "Updating $apt_root/$dir ..."

            # generate the "Packages"
            dpkg-scanpackages $dir /dev/null                        > $dir/Packages
            dpkg-scanpackages $dir /dev/null  2>/dev/null | gzip -9 > $dir/Packages.gz
            dpkg-scanpackages $dir /dev/null  2>/dev/null | bzip2   > $dir/Packages.bz2
        done
    done
    
    # generate the Release and Release.gpg
    if [ -d dists/$d ]; then
        rm -f dists/$d/Release* 2>/dev/null || true
        if [ -f ${aptconf}_${d} ]; then
            echo
            echo "Updating $apt_root/dists/$d/Release and $apt_root/dists/$d/Release.gpg ..."
            apt-ftparchive -c ${aptconf}_${d} release $apt_root/dists/$d > dists/$d/Release 
            # to run from cron
            gpg --batch --passphrase cHJheWF5Y --sign -ba -o dists/$d/Release.gpg dists/$d/Release
            #gpg --no-tty --passphrase cHJheWF5Y --sign -ba -o dists/$d/Release.gpg dists/$d/Release
        fi
    fi
    
done
